package com.xinchao.ck_mips.dao;

import com.xinchao.ck_mips.bean.OffLineData;
import com.xinchao.ck_mips.bean.ProgramBean;

import java.util.List;

/**
 * Created by wq
 * 数据表操作类
 */

public class DaoUtil {
    public static ProgramBeanDao getProgramDao() {
        return DaoHelper.getDaoSession().getProgramBeanDao();
    }

    public static OffLineDataDao getOffLineDao() {
        return DaoHelper.getDaoSession().getOffLineDataDao();
    }

    /**
     * 添加数据到表中
     *
     * @param bean
     */
    public static void insertDB(ProgramBean bean) {
        getProgramDao().insertOrReplace(bean);
    }

    /**
     * 保存离线数据到数据表中
     *
     * @param bean
     */
    public static void insertDB(OffLineData bean) {
        getOffLineDao().insertOrReplace(bean);
    }

    public static List<OffLineData> getOffLineData() {
        return getOffLineDao().queryBuilder().build().list();
    }

    public static List<ProgramBean> getProgramData() {
        return getProgramDao().queryBuilder().build().list();
    }

    /**
     * 根据节目版本号，删除数据
     *
     * @param version 节目版本号
     */
    public static void deleteByKey(String version) {
        getProgramDao().queryBuilder().where(ProgramBeanDao.Properties.ProgramVersion.eq(version)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    /**
     * 根据节目版本号返回节目信息
     *
     * @param version
     * @return
     */
    public static ProgramBean queryDataByKey(String version) {
        List<ProgramBean> mInfo = getProgramDao().queryBuilder().where(ProgramBeanDao.Properties.ProgramVersion.eq(version)).list();
        if (mInfo != null && mInfo.size() > 0) {
            return mInfo.get(0);
        } else {
            return null;
        }
    }

    /**
     * 根据时间戳删除离线数据
     *
     * @param timestamp
     */
    public static void deleteByKey(long timestamp) {
        getOffLineDao().queryBuilder().where(OffLineDataDao.Properties.Timestamp.eq(timestamp)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    /**
     * 删除数据所有数据
     */
    public static void deleteProgramData() {
        getProgramDao().deleteAll();
    }

}
